// components/month-picker/month-picker.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    currDate: {
      type: String,
      value: ''
    },
    startTime: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selectedMonth: '',
    monthArr: '',
    currMonthArr: [],
    year: '',
    startYear: '',
    startMonth: '',
    todayYear:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 切换月历
    changeYear(e) {
      let day = new Date()
      let month = day.getMonth() + 1;
      if (e.target.dataset.state == 'desc') {
        // 年份减小

        let currMonthArr = []
        if (this.data.selectedMonth == this.data.startMonth) {
          for (let i = this.data.startMonth; i <= 12; i++) {
            currMonthArr.push(i)
          }
        } else {
          for (let i = 1; i <= 12; i++) {
            currMonthArr.push(i)
          }
        }

        this.setData({
          year: this.data.year - 1,
          monthArr: currMonthArr,
          selectedMonth: 12
        })
      } else {
        // 年份增加
        if (this.data.year < day.getFullYear()) {
          this.setData({
            year: this.data.year + 1
          })
          if (this.data.year == day.getFullYear()) {
            let currMonthArr = []
            for (let i = 1; i < month; i++) {
              currMonthArr.push(i)
            }
            this.setData({
              monthArr: currMonthArr,
              selectedMonth: (month - 1 + '').length == 1 ? '0' + (month - 1 + '') : (month - 1)
            })
          } else {
            let currMonthArr = []
            for (let i = 1; i <= 12; i++) {
              currMonthArr.push(i)
            }
            this.setData({
              monthArr: currMonthArr,
              selectedMonth: 12
            })
          }
        }
      }
    },
    changeMonthItem(e) {
      this.setData({
        selectedMonth: (e.target.dataset.day + '').length == 1 ? '0' + e.target.dataset.day : e.target.dataset.day,
        isCanlenderMonthOpen: false
      })
      let obj = {
        year: this.data.year,
        selectedMonth: (this.data.selectedMonth + '').length == 1 ? '0' + this.data.selectedMonth : this.data.selectedMonth
      }
      this.triggerEvent("getMonthDate", obj)
    }
  },
  ready() {
    this.setData({
      year: Number(this.data.currDate.substring(0, 4)),
      selectedMonth: Number(this.data.currDate.substring(5, 7)),
      startYear: Number(this.data.startTime.substring(0, 4)),
      startMonth: Number(this.data.startTime.substring(5, 7))
    })
    let day = new Date();
    let currYear = day.getFullYear();
    this.setData({
      todayYear:currYear
    })
    let currMonthArr = []
    if (currYear == this.data.year) {
      let month = day.getMonth() + 1;
      if(this.data.startYear == this.data.year){
        for (let i = this.data.startMonth; i < month; i++) {
          currMonthArr.push(i)
        }
      }else{
        for (let i = 1; i < month; i++) {
          currMonthArr.push(i)
        }
      }
      this.setData({
        monthArr: currMonthArr
      })

    } else {
      for (let i = 1; i <= 12; i++) {
        currMonthArr.push(i)
      }
      this.setData({
        monthArr: currMonthArr
      })
    }
  }
})